import '../../interface/chat_room_leaf_interface.dart';

class UserListPageCashModel extends ChatRoomChangeNotifier {
  int initCashIndex;
  bool isCurrUserRanking = false;
  // SimpleChannelUser? lastUser;

  UserListPageCashModel(this.initCashIndex) {
    try {
      selectItem(initCashIndex);
    } catch (e) {}
  }

  @override
  observeMessage(int messageId, data) {}

  @override
  onRootProxyModelCreated() {
    _requestData(initCashIndex);
  }

  // Map<int, ChannelUserListRsp?> dataMap = new HashMap();
  int currentIndex = 0;

  selectItem(int index) {
    currentIndex = index;
    _requestData(index);
  }

  _requestData(int index) async {
    // showLoadingDialog();
    // var data = (await RtcApi.instance.getChannelUserList(
    //         roomModel.channel!.channelId, 0, 200,
    //         scope: getScopeWithId(getIdWithIndex(index))))
    //     .data;
    // dismissLoadingDialog();
    // dataMap[index] = data;
    _currUserRanking();
    notifyListeners();
  }

  _currUserRanking() {
    // ChannelUserListRsp? data = dataMap[currentIndex];
    // if (data != null) {
    //   SimpleChannelUser current = data.myself;
    //   // rank = -1 不在排行榜
    //   isCurrUserRanking = data.myself.rank >= 0;
    //   if (isCurrUserRanking) {
    //     // 在榜单,取他前一个用户
    //     if (current.rank > data.users.length || current.rank <= 1) {
    //       lastUser = null;
    //     } else {
    //       lastUser = data.users.elementAt(current.rank - 2);
    //     }

    //   } else {
    //     //不在排行榜
    //     if (data.users.isNotEmpty) {
    //       lastUser = data.users.last;
    //     } else {
    //       lastUser = null;
    //     }
    //   }
    // }
  }

  int getIdWithIndex(int index) {
    if (index == 0) {
      return 1;
    } else if (index == 1) {
      return 2;
    } else if (index == 2) {
      return 3;
    }
    return 1;
  }

  String getScopeWithId(int id) {
    switch (id) {
      case 0:
        return "";
      case 1:
        return "cash_day";
      case 2:
        return "cash_week";
      case 3:
        return "cash_month";
      case 4:
        return "admin";
    }
    return "";
  }

  getDataWithIndex(int index) {
    // if (dataMap[index] != null) {
    //   return dataMap[index];
    // }
    _requestData(index);
  }

  // onItemPressed(BuildContext context, SimpleChannelUser item) {
  //   RoomPersonalDataWidget.show(context, roomModel.channel, item);
  // }
}
